package com.aeye.mbr.ws;

import com.aeye.mbr.common.util.SpringContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Servlet模拟WebService
 */
public class WebService extends HttpServlet {
	private static final String WSDL ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
			+"<wsdl:definitions xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:tns=\"http://webservice.ws.mbr.aeye.com/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:ns1=\"http://schemas.xmlsoap.org/soap/http\" name=\"MainServletImplService\" targetNamespace=\"http://webservice.ws.mbr.aeye.com/\">"
			+"	<wsdl:types>"
			+"		<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:tns=\"http://webservice.ws.mbr.aeye.com/\" elementFormDefault=\"unqualified\" targetNamespace=\"http://webservice.ws.mbr.aeye.com/\" version=\"1.0\">"
			+"			<xs:element name=\"invokews\" type=\"tns:invokews\"/>"
			+"			<xs:element name=\"invokewsResponse\" type=\"tns:invokewsResponse\"/>"
			+"			<xs:complexType name=\"invokews\">"
			+"				<xs:sequence>"
			+"					<xs:element minOccurs=\"0\" name=\"arg0\" type=\"xs:string\"/>"
			+"				</xs:sequence>"
			+"			</xs:complexType>"
			+"			<xs:complexType name=\"invokewsResponse\">"
			+"				<xs:sequence>"
			+"					<xs:element minOccurs=\"0\" name=\"return\" type=\"xs:string\"/>"
			+"				</xs:sequence>"
			+"			</xs:complexType>"
			+"		</xs:schema>"
			+"	</wsdl:types>"
			+"	<wsdl:message name=\"invokews\">"
			+"		<wsdl:part element=\"tns:invokews\" name=\"parameters\">"
			+"		</wsdl:part>"
			+"	</wsdl:message>"
			+"	<wsdl:message name=\"invokewsResponse\">"
			+"		<wsdl:part element=\"tns:invokewsResponse\" name=\"parameters\">"
			+"		</wsdl:part>"
			+"	</wsdl:message>"
			+"	<wsdl:portType name=\"MainServlet\">"
			+"		<wsdl:operation name=\"invokews\">"
			+"			<wsdl:input message=\"tns:invokews\" name=\"invokews\">"
			+"			</wsdl:input>"
			+"			<wsdl:output message=\"tns:invokewsResponse\" name=\"invokewsResponse\">"
			+"			</wsdl:output>"
			+"		</wsdl:operation>"
			+"	</wsdl:portType>"
			+"	<wsdl:binding name=\"MainServletImplServiceSoapBinding\" type=\"tns:MainServlet\">"
			+"		<soap:binding style=\"document\" transport=\"http://schemas.xmlsoap.org/soap/http\"/>"
			+"		<wsdl:operation name=\"invokews\">"
			+"			<soap:operation soapAction=\"\" style=\"document\"/>"
			+"			<wsdl:input name=\"invokews\">"
			+"				<soap:body use=\"literal\"/>"
			+"			</wsdl:input>"
			+"			<wsdl:output name=\"invokewsResponse\">"
			+"				<soap:body use=\"literal\"/>"
			+"			</wsdl:output>"
			+"		</wsdl:operation>"
			+"	</wsdl:binding>"
			+"	<wsdl:service name=\"MainServletImplService\">"
			+"		<wsdl:port binding=\"tns:MainServletImplServiceSoapBinding\" name=\"MainServletImplPort\">"
			+"			<soap:address location=\"http://_sname:_port/_cpath/services/MainServlet\"/>"
			+"		</wsdl:port>"
			+ "	</wsdl:service>"
			+ "</wsdl:definitions>";

	private static final Logger log = LoggerFactory
			.getLogger(WebService.class);
	/**
	 * get 方法,不处理，只返回wsdl
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String sname = req.getServerName();
		String port = Integer.toString(req.getServerPort());
		String cpath =  req.getContextPath().trim();
		String wsdl = WSDL.replaceFirst("_sname", sname).replaceFirst("_port", port).replaceFirst("_cpath", cpath);
		Enumeration<String> parameter = req.getParameterNames();
		if(parameter.hasMoreElements()){
			if (parameter.nextElement().equalsIgnoreCase("wsdl")) {
				resp.setContentType("text/xml;charset=utf-8");   
				resp.setCharacterEncoding("UTF-8");  
				resp.setHeader("Cache-Control", "no-cache"); 
				resp.getWriter().write(wsdl);//对应wsdl位置
			}else{
				resp.setStatus(404);
			}
		}else{
			resp.setStatus(404);
		}
		
	}

	/**
	 * 业务处理请求
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
			throws ServletException, IOException {
		((ThreadPoolTaskExecutor) SpringContextUtil.getBean("webServiceExecutor")).execute(new ServiceExecutor(req.startAsync()));
	}
}
